package com.yanqu.road.dao.impl.activity.xiayue;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiayue.XiaYueUserData;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XiaYueUserDataDaoImpl extends TempDataObjectDao<XiaYueUserData> {

    public static String getXiaYueUserDataSql = "select * from `t_u_activity_xiayue_user_data` where `activityId`=? and `userId`=?";

    public XiaYueUserData getXiaYueUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getXiaYueUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateXiaYueUserDataSql = "update `t_u_activity_xiayue_user_data` set score=?,animalProp=?,npcList=?,commonTimes=?,curPlace=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(XiaYueUserData xiaYueUserData) {
        xiaYueUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(xiaYueUserData.getAnimalProp())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiaYueUserData.getNpcList())));
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getCurPlace()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiaYueUserData.getUserId()));
        if (getDb().executeNoneQuery(updateXiaYueUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertXiaYueUserDataSql = "insert into `t_u_activity_xiayue_user_data`(activityId,userId,score,animalProp,npcList,commonTimes,curPlace) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(XiaYueUserData xiaYueUserData) {
        xiaYueUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiaYueUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(xiaYueUserData.getAnimalProp())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiaYueUserData.getNpcList())));
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaYueUserData.getCurPlace()));
        if (getDb().executeNoneQuery(insertXiaYueUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected XiaYueUserData parseData(ResultSet rs) throws SQLException {
        XiaYueUserData xiaYueUserData = new XiaYueUserData();
        xiaYueUserData.setActivityId(rs.getInt("activityId"));
        xiaYueUserData.setUserId(rs.getLong("userId"));
        xiaYueUserData.setScore(rs.getInt("score"));
        xiaYueUserData.setAnimalProp(PropertyHelper.parseStringToProperty(rs.getString("animalProp")));
        xiaYueUserData.setNpcList(JSON.parseObject(rs.getString("npcList"), new TypeReference<List<Integer>>() {
        }));
        xiaYueUserData.setCommonTimes(rs.getInt("commonTimes"));
        xiaYueUserData.setCurPlace(rs.getInt("curPlace"));
        return xiaYueUserData;
    }

}
